<?php

namespace App\Http\Controllers\Api;

use App\Models\GoodsBrand;
use App\Models\GoodsCat;
use Illuminate\Http\Request;

class CategoriesController extends Controller
{
    public function index()
    {
        return $this->success(['items' => GoodsCat::getCategoriesByParentId()]);
    }

    public function show(GoodsCat $goodsCat)
    {
        $key = config('app.category_cache_prefix') . $goodsCat->cat_id;
        $minutes = config('app.category_cache_ttl');
        $items = \Cache::remember($key, $minutes, function () use ($goodsCat) {
            return $goodsCat->getChildrenTree()->toArray();
        });

        return $this->success(['items' => $items]);
    }

    public function recommendBrands()
    {
        $brands = GoodsBrand::enableRecommendBrands();

        return $this->success([
            'page' => $brands->currentPage(),
            'total' => $brands->lastPage(),
            'totalSize' => $brands->total(),
            'records' => $brands->total(),
            'items' => $brands->items()
        ]);
    }
}
